package utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;

public class Parameters {

	// 上传文件存放地址
	public final static String UPLOADFILEPATH = "UPLOADDOC";

	// public final static String EQUIPTEMPPATH = "EQUIPTEMP";
	/**
	 * 获取参数 上传操作 特有的key值 前三个 struts2特有的属性
	 * 
	 * @param fileFileName
	 *            文件名称
	 * @param fileContentType
	 *            文件类型
	 * @param file
	 *            上传的文件
	 * @param Filename
	 *            文件名称
	 * @param Upload
	 *            Submit Query
	 */
	public static Map<String, Object> getUploadParameters() {
		Map<String, Object> m = new HashMap<String, Object>();
		ActionContext context = ActionContext.getContext();
		Set set = context.getParameters().keySet();
		Iterator ite = set.iterator();
		while (ite.hasNext()) {
			Object men = ite.next();
			Object[] param = (Object[]) context.getParameters().get(
					men.toString());
			if (param.length > 1) {
				String para = "";
				for (int i = 0; i < param.length; i++) {
					if (i == param.length - 1) {
						para += param[i];
					} else {
						para += param[i] + ",";
					}
				}
				m.put(men.toString(), para);
			} else {
				m.put(men.toString(), param[0]);
			}
		}
		return m;
	}

	/**
	 * 封装参数
	 * 
	 * @param url
	 * @return
	 */
	public static String getUrl(Map<String, Object> url) {
		Set set = url.keySet();
		Iterator ite = set.iterator();
		String para = "";
		int count = 0;
		while (ite.hasNext()) {
			Object men = ite.next();
			count++;
			String menStr = "";
			try {
				menStr = java.net.URLEncoder.encode(java.net.URLEncoder.encode(
						String.valueOf(url.get(men)), "UTF-8"), "UTF-8");
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			if (count == set.size()) {
				para += men + "=" + menStr;
			} else {
				para += men + "=" + menStr + "&";
			}

		}
		return para;
	}

	public static void setMapAttribute(HttpServletRequest request,
			Map<String, Object> map) {
		if (null != map && null != map.get("URLMEMORY")) {
			request.setAttribute("URLMEMORY", map.get("URLMEMORY"));
			map.put("URLMEMORY", map.get("URLMEMORY"));
		} else {
			String url = Parameters.getUrl(map);
			request.setAttribute("URLMEMORY", url);
			map.put("URLMEMORY", url);
		}
	}

	public static String[] getPageSizeNo(Map<String, Object> map) {
		String[] res = new String[2];
		if (null != map && null != map.get("pageSize")
				&& !"".equals(map.get("pageSize"))) {
			int pageSize = Integer
					.parseInt(String.valueOf(map.get("pageSize")));
			if (null != map && null != map.get("rowTotal")
					&& "delete".equals(String.valueOf(map.get("operate")))) {
				int rowTotal = Integer.parseInt(String.valueOf(map
						.get("rowTotal")));
				if (rowTotal != 0) {
					int mod = rowTotal % pageSize;
					if (mod == 1) {
						int currenPage = Integer.parseInt(String.valueOf(map
								.get("d-2677329-p")));
						// map.put("d-2677329-p", currenPage-1);
						// super.setPageNo(currenPage-1);
						res[0] = String.valueOf(currenPage - 1);
					}
				}
			}
			// super.setPageSize(pageSize);
			res[1] = String.valueOf(pageSize);
		}
		return res;
	}

	/**
	 * 去除数组中重复记录
	 * 
	 * @param a
	 * @return
	 */
	public static String[] arrayUnique(String[] a) {
		List<String> list = new LinkedList<String>();
		for (int i = 0; i < a.length; i++) {
			if (!list.contains(a[i])) {
				list.add(a[i]);
			}
		}
		return (String[]) list.toArray(new String[list.size()]);
	}

	public static void main(String args[]) {
		String regx = "(([\u4E00-\u9FA5]+)|([a-zA-Z]))";
	}

	

}
